#include "stm32f10x.h"                  // Device header

#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "voice.h"
#include "Face_Config.h"

uint16_t Action_Mode = 0;
uint16_t Face_Mode = 0;
uint8_t WeiBa = 0;

/*
 * 语音的输入输出是UART1,
 * UART1_RX:PA10 , UART1_TX:PA9
*/
void voice_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructer;
    GPIO_InitStructer.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructer.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructer.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructer);
    GPIO_InitStructer.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructer.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructer);

    USART_InitTypeDef USART_InitStructer; //此配置和语音网站的配置相同
    USART_InitStructer.USART_BaudRate = 9600;
    USART_InitStructer.USART_WordLength = USART_WordLength_8b;
    USART_InitStructer.USART_StopBits = USART_StopBits_1;
    USART_InitStructer.USART_Parity = USART_Parity_No;
    USART_InitStructer.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructer.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &USART_InitStructer);
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    NVIC_InitTypeDef NVIC_InitStructer;
    NVIC_InitStructer.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructer.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructer.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructer.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructer);

    USART_Cmd(USART1, ENABLE);
}

void USART1_IRQHandler(void)
{
    if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) {
        if (USART_ReceiveData(USART1) == 0xA1) { //立正 
            Face_Mode = enum_attention;
            Face_Config();
            Action_Mode = enum_attention;
        }
        if (USART_ReceiveData(USART1) == 0xA2) { //叫 
            Face_Mode = enum_cry;
            Face_Config();
            Action_Mode = enum_cry;
        }
        if (USART_ReceiveData(USART1) == 0xA3) { //睡觉 
            Face_Mode = enum_sleep;
            Face_Config();
            Action_Mode = enum_sleep;
        }
        if (USART_ReceiveData(USART1) == 0xA4) { //趴下 
            Face_Mode = enum_fall;
            Face_Config();
            Action_Mode = enum_fall;
        }
        if (USART_ReceiveData(USART1) == 0xA5) { //蹲下 
            Face_Mode = enum_squat;
            Face_Config();
            Action_Mode = enum_squat;
        }
        if (USART_ReceiveData(USART1) == 0xA6) { //摇尾巴 
            Face_Mode = enum_wag_tail;
            Face_Config();
            Action_Mode = enum_wag_tail;
        }
        if (USART_ReceiveData(USART1) == 0xA7) { //摇摆 
            Face_Mode = enum_swing;
            Face_Config();
            Action_Mode = enum_swing;
        }
        if (USART_ReceiveData(USART1) == 0xA8) { //握手 
            Face_Mode = enum_handshake;
            Face_Config();
            Action_Mode = enum_handshake;
        }
        if (USART_ReceiveData(USART1) == 0xA9) { //坐下 
            Face_Mode = enum_sit;
            Face_Config();
            Action_Mode = enum_sit;
        }
        if (USART_ReceiveData(USART1) == 0xB1) { //前进 
            Face_Mode = enum_forward;
            Face_Config();
            Action_Mode = enum_forward;
        }
        if (USART_ReceiveData(USART1) == 0xB2) { //后退 
            Face_Mode = enum_retreat;
            Face_Config();
            Action_Mode = enum_retreat;
        }
        if (USART_ReceiveData(USART1) == 0xB3) { //左转 
            Face_Mode = enum_turn_left;
            Face_Config();
            Action_Mode = enum_turn_left;
        }
        if (USART_ReceiveData(USART1) == 0xB4) { //右转 
            Face_Mode = enum_turn_right;
            Face_Config();
            Action_Mode = enum_turn_right;
        }
        if (USART_ReceiveData(USART1) == 0xB5) { //向前跳 
            Face_Mode = enum_jump_up;
            Face_Config();
            Action_Mode = enum_jump_up;
        }
        if (USART_ReceiveData(USART1) == 0xB6) { //向后跳 
            Face_Mode = enum_jump_down;
            Face_Config();
            Action_Mode = enum_jump_down;
        }

        USART_ClearITPendingBit(USART1, USART_IT_RXNE);

    }
}
